/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */
package com.yami.shop.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.yami.shop.bean.model.CategoryBook;
// import com.yami.shop.dao.CategoryBookBrandMapper;
import com.yami.shop.dao.CategoryBookMapper;
// import com.yami.shop.dao.CategoryBookPropMapper;
import com.yami.shop.service.AttachFileService;
import com.yami.shop.service.CategoryBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
 * @author lanhai
 */
@Service
public class CategoryBookServiceImpl extends ServiceImpl<CategoryBookMapper, CategoryBook> implements CategoryBookService{

	@Autowired
	private CategoryBookMapper categoryBookMapper;

	// @Autowired
	// private CategoryBookBrandMapper categoryBookBrandMapper;

	// @Autowired
	// private CategoryBookPropMapper categoryBookPropMapper;

	@Autowired
	private AttachFileService attachFileService;

	@Override
	public List<CategoryBook> listByFid(Long fid) {
		return categoryBookMapper.listByFid(fid);
	}

	@Override
	public List<CategoryBook> tableCategoryBook() {
		return categoryBookMapper.tableCategoryBook();
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveCategoryBook(CategoryBook categoryBook) {
		// 保存分类信息
		categoryBookMapper.insert(categoryBook);

		insertBrandsAndAttributes(categoryBook);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateCategoryBook(CategoryBook categoryBook) {
		CategoryBook dbCategoryBook = categoryBookMapper.selectById(categoryBook.getId());
		// 保存分类信息
		categoryBookMapper.updateById(categoryBook);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteCategoryBook(Long categoryBookId) {
		CategoryBook categoryBook = categoryBookMapper.selectById(categoryBookId);
		categoryBookMapper.deleteById(categoryBookId);

		deleteBrandsAndAttributes(categoryBookId);
//		if (StrUtil.isNotBlank(categoryBook.getPic())) {
//			attachFileService.deleteFile(categoryBook.getPic());
//		}
	}


	private void deleteBrandsAndAttributes(Long categoryBookId) {
		// 删除所有分类所关联的品牌
		// categoryBookBrandMapper.deleteByCategoryBookId(categoryBookId);
		// 删除所有分类所关联的参数
		// categoryBookPropMapper.deleteByCategoryBookId(categoryBookId);
	}

	private void insertBrandsAndAttributes(CategoryBook categoryBook) {
		//保存分类与品牌信息
		// if(CollUtil.isNotEmpty(categoryBook.getBrandIds())){
		// 	categoryBookBrandMapper.insertCategoryBookBrand(categoryBook.getCategoryBookId(), categoryBook.getBrandIds());
		// }

		// //保存分类与参数信息
		// if(CollUtil.isNotEmpty(categoryBook.getAttributeIds())){
		// 	categoryBookPropMapper.insertCategoryBookProp(categoryBook.getCategoryBookId(), categoryBook.getAttributeIds());
		// }
	}

	@Override
	public List<CategoryBook> treeSelect(Long id,int grade) {
    	List<CategoryBook> categories = categoryBookMapper.selectList(new LambdaQueryWrapper<CategoryBook>().eq(CategoryBook::getId,id));
		return categoryBookListToTree(categories);
	}

	public List<CategoryBook> categoryBookListToTree(List<CategoryBook> categoryBooks){
		if (CollectionUtils.isEmpty(categoryBooks)) {
			return Lists.newArrayList();
		}
		Map<Long, List<CategoryBook>> categoryBookMap = categoryBooks.stream().collect(Collectors.groupingBy(CategoryBook::getFid));

		List<CategoryBook> rootList = categoryBookMap.get(0L);
		transformCategoryBookTree(rootList,categoryBookMap);
		return rootList;
	}

	public void transformCategoryBookTree(List<CategoryBook> categoryBooks,Map<Long, List<CategoryBook>> categoryBookMap) {
		for (CategoryBook categoryBook : categoryBooks) {
			List<CategoryBook> nextList = categoryBookMap.get(categoryBook.getId());
			if (CollectionUtils.isEmpty(nextList)) {
				continue;
			}
			// 将排序完成的list设为下一层级
			// categoryBook.setCategories(nextList);
			// 处理下 下一层级
			transformCategoryBookTree(nextList, categoryBookMap);
		}
	}

}
